<?php
/****
** 业务列表
** 
**/
class BusinessAction extends AuthAction{

    //业务列表	10-12,gqh新增获取货号,通过货号进行模糊查询
	public function index(){
		if(empty($_SESSION['topadmin'])){
			$where['store_id']=session('afid');
		}
        if($_GET['business_name']){
			$where['business_name']=array('like',$_GET['business_name'].'%');
		}

        if(isset($_GET['business_type_id'])){
            if($_GET['business_type_id']!='all'){
                $where['business_type_id']=$_GET['business_type_id'];
            }
        }else{
            $_GET['business_type_id']='all';
        }
		if(isset($_GET['is_on_sale'])){
			if($_GET['is_on_sale']!='all'){
				$where['is_on_sale']=$_GET['is_on_sale'];
			}
		}else{
			$_GET['is_on_sale']='all';
		}
        $where['pid']="neq 0";
        $REQUEST_URI=$_SERVER['REQUEST_URI'];
        cookie('now_url',$REQUEST_URI);

        // 业务分类
        $businessType = M('business_type')->select();
        $this->businessType = $businessType;

        // 业务列表
        $where['is_delete']=0;
        $menulist=D("Common")->getPageList('business',$where,"*","id desc");

        foreach ($menulist['list'] as $k => $v  ) {
            $business_type = '';
            foreach ($businessType as $k_ => $v_ ) {
                if ($v_['id'] === $v['business_type_id']) {
                    $business_type = $v_['name'] ;
                }
            }
            $menulist['list'][$k]['business_type'] = $business_type;
        }
        $this->assign("list",$menulist);

		$this->display();
  }
  

	//编辑用户信息 update hkj 分类修改后 货号也修改

    /**
     *
     */
    public function business_edit(){
		if ($_POST){
            $businessData = array();
            $businessData['business_type_id'] = $_POST['business_type_id'];
            $businessData['business_name'] = $_POST['business_name'];
            $businessData['content'] = $_POST['content'];
            $businessData['is_on_sale'] = $_POST['is_on_sale'];
            $businessData['is_show_index'] = $_POST['is_show_index'];
            $businessData['sort'] = $_POST['sort'];
            $businessData['price'] = $_POST['price'];
            $businessData['has_child'] = $_POST['has_child'];
            $businessData['inside_info'] =  $_POST['inside_info'] ? json_encode($_POST['inside_info']): '';
            $businessData['outside_info'] = $_POST['outside_info'] ? json_encode($_POST['outside_info']): '';
            $businessData['explain'] = $_POST['explain'] ? json_encode($_POST['explain']) : '';
            $businessData['update_time'] = time();
            if ($_POST['id']) {
                $res = M('Business')->where(array('id'=> $_POST['id']))->save($businessData);
            } else {
                $businessData['add_time'] = time();
                $res = M('Business')->add($businessData);
            }
            $return_data = array(
                "code" => 202,
                "msg" => '保存失败',
                "data" => '保存失败',
            );
            if ($res !== false) {
                $return_data = array(
                    "code" => 200,
                    "msg" => "{$businessData['business_name']}保存成功",
                    "data" => array(
                        "id" => $res,
                    ),
                );
            }

            echo json_encode($return_data, true);
            exit;
		} else {
			if($_GET['id']){
			    $businessId = $_GET['id'];
			    // 业务信息
                $data_where["id"] =  $businessId;
                $data = M('business')->where($data_where)->find();

                // 子业务列表
                $children_where["pid"] = $businessId;
                $children_list=D("Common")->getPageList('business',$children_where,"*","id desc");
                $this->assign("childrenList", $children_list);
			    $this->assign("data",$data);
			}
			//商品分类
			//$Category=D('Common')->getAllList('g_category',array('is_show'=>1));
			$Category=D('Category')->getCategoryAll(array('is_show'=>1));
			$Category=$this->gettree($Category,0,0,'parent_id','cat_id');
            $this->assign("category",$Category);

            // 业务分类
            $businessType = M('business_type')->select();
            $this->businessType = $businessType;
			//商品品牌
            $brand=D('Brand')->getBrandAll(array('is_show'=>1));
            $this->assign("brand",$brand);
           //商品类型 
            $this->goods_type=M('g_goods_type')->where(array('enabled'=>1))->select();
			$title="商品信息";
			$this->assign("msgtitle",$title);
			$this->display();
		}
	}


	// 编辑子业务 接口
     public  function child_business_edit () {
        if ($_POST) {
            $pid = $_GET['pid'];
            $businessData = array();
            // child_business_name child_price child_is_on_sale child_info[]
            $businessData['business_name'] = $_POST['child_business_name'];
            $businessData['price'] = $_POST['child_price'];
            $businessData['is_on_sale'] = $_POST['child_is_on_sale'];
            $businessData['inside_info'] =  $_POST['child_info'] ? json_encode($_POST['child_info']): '';
            $businessData['sort'] = $_POST['child_sort'];
            $businessData['update_time'] = time();
            if ($_POST['child_id']) {
                $res = M('Business')->where(array('id'=> $_POST['child_id']))->save($businessData);
            } else {
                $businessData['add_time'] = time();
                $businessData['has_child'] = 0;
                $businessData['pid'] = $pid;
                $res = M('Business')->add($businessData);
            }
            $return_data = array(
                "code" => 202,
                "msg" => '保存失败',
                "data" => '保存失败',
            );
            if ($res !== false) {
                $return_data = array(
                    "code" => 200,
                    "msg" => "{$businessData['business_name']}保存成功",
                    "data" => $res,
                    "req" => $businessData['inside_info'],
                );
            }
            echo json_encode($return_data);
            exit;
        }
     }

    // 获取子业务列表
    public function getChildList (){
        $where['pid']=$_POST['pid'];
        $where['is_delete']= 0;
        $_POST['is_on_sale'] && $where['is_on_sale']=$_POST['is_on_sale'];
        $dataList = M('Business')->where($where)->select();
        $return_data = array(
            "code" => 202,
            "msg" => "查询失败",
            "data" => array(),
        );
        if ($dataList) {
            $return_data = array(
                "code" => 200,
                "msg" => "查询成功",
                "data" => $dataList,
            );
        }
        echo json_encode($return_data);
        exit;
    }

    // 获取某子业务的信息
    public function getChildInfo () {
        $where['id']=$_POST['id'];
        $data =M('Business')->where($where)->find();
        $data['test'] = json_decode(data['inside_info']);
        $return_data = array(
            "code" => 200,
            "msg" => "查询成功",
            "data" => $data,
        );
        echo json_encode($return_data, true);
        exit;
    }

    ///删除子业务
    public function childBusinessDel(){
        $where['id']=$_POST['id'];
        $data['is_delete']=1;
        $res=M('Business')->where($where)->save($data);
        $return_data = array(
            "code" => 202,
            "msg" => "删除失败",
            "data" => array(),
        );
        if ($res) {
            $return_data = array(
                "code" => 200,
                "msg" => "删除成功",
                "data" => $res,
            );
        }
        echo json_encode($return_data);
        exit;
    }

     ///删除业务
     public function business_del(){
     	$where['id']=$_GET['id'];
     	$data['is_delete']=1;
         $res=M('Business')->where($where)->save($data);
        if($res!==false){
        	$this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
     }


	//修改上下架
	public function is_on_sale(){
		$data['status'] = 0;
		if($_GET['value'] !=null){
			$id = $_GET["id"];
			$is_on_sale = $_GET["value"];
			$where['id']=$id;
			$save_date['is_on_sale']=$is_on_sale;
			$res=M('Business')->where($where)->save($save_date);
            $return_data = array(
                "code" => 202,
                "msg" => "删除失败",
                "data" => array(),
            );
            if ($res) {
                $return_data = array(
                    "code" => 200,
                    "msg" => "删除成功",
                    "data" => $res,
                );
            }
            echo json_encode($return_data);
            exit;
		}
	}

}
?>